using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using hemsidan.Entities;

namespace hemsidan.change
{
	/// <summary>
	/// Summary description for ManageGalleries.
	/// </summary>
	public partial class ManageGalleries : Bases.Page
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			lblMappedGalleryRoot.Text = Server.MapPath(Helpers.Settings.VirtualRoot + "/Gallery");
			if (!IsPostBack)
			{
				LoadData();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.myDgGalleries.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.myDgGalleries_ItemCommand);

		}
		#endregion


		private void LoadData()
		{
			DataTable galleries = Entities.PhotoGallery.GetAllGalleries();
			myDgGalleries.DataSource = galleries;
			myDgGalleries.DataBind();
			txtId.Text = "0";
			txtName.Text ="";
			txtDescription.Text = "";
		}

		protected void btnSave_Click(object sender, System.EventArgs e)
		{
			ImageFile image = new ImageFile(flZip.PostedFile.FileName, flZip.PostedFile.ContentType, flZip.PostedFile.InputStream);
			if (txtId.Text == "0")
			{
				Entities.PhotoGallery.CreateGallery	(txtName.Text, txtDescription.Text, image);
			}
			else
			{
				if (flZip.PostedFile.ContentLength == 0)
					Entities.PhotoGallery.UpdateGallery(txtId.Text, txtName.Text, txtDescription.Text);
				else
					Entities.PhotoGallery.UpdateGallery(txtId.Text, txtName.Text, txtDescription.Text, image);
			}
			LoadData();
		}

		private void myDgGalleries_ItemCommand(object source, DataGridCommandEventArgs e)
		{
			string GalleryId = Convert.ToString(myDgGalleries.DataKeys[e.Item.ItemIndex]);
			if (e.CommandName == "EditMeta")
			{
				txtId.Text = GalleryId;
				txtName.Text = e.Item.Cells[0].Text;
				txtDescription.Text = e.Item.Cells[1].Text;
			}
			else if (e.CommandName == "Delete")
			{
				Entities.PhotoGallery.DeleteGallery(GalleryId);
				LoadData();
			}
			
		}
	}
}
